<?php
/**
 * Created by dutuwang.net
 * User: adophper <hello@adophper.com>
 * Date: 2018/4/20
 * Time: 10:46
 */

use \yii\helpers\Html;

?>
<?=Html::jsFile('/static/common/js/ajaxfileupload.js')?>
<style>
.import-status {
    margin: 20px;
    height: 150px;
    font-size: 16px;
    font-weight: bold;
    overflow: hidden;
}
</style>
<div class="import">
    <p><a href="/upload/template.xlsx" target="_blank">模板下载</a></p>
    <div class="form-group">
        <div class="hidden">
            <input type="file" name="Filedata" class="file_upload_attach" id="upload_child_attach" data-id="file_path">
        </div>
        <div class="input-group">
            <span class="input-group-addon" style="border-color: transparent;">请选择要上传的文件：</span>
            <input type="text" class="form-control" id="file_path" readonly name="filepath" />
            <div class="input-group-addon" style="padding: 0; border-color: transparent;">
                <button class="btn btn-large btn-primary" id="select_file">选择上传</button>
                <button class="btn btn-large btn-success" id="submit_btn">确定上传</button>
            </div>
        </div>
    </div>
    <div class="import-status">
        <p class="text-success hidden" id="msg_success">上传成功100条。</p>
        <p class="text-danger hidden" id="msg_failure">失败5条！</p>
    </div>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
</div>
<script>
$(function () {
    $("#select_file").unbind('click').bind('click', function (e) {
        $("#upload_child_attach").trigger('click');
    })
    $("#upload_child_attach").unbind('change').bind('change', function (e) {
        var val = e.target.value;
        var point = val.lastIndexOf(".");
        var type = val.substr(point).toLowerCase();
        if (type != ".xlsx" && type != '.xls') {
            alert('系统目前只支持.xlsx、.xls文件格式');
            return false;
        }
        $.ajaxFileUpload({
            //处理图片的脚本路径
            url: '<?=\yii\helpers\Url::toRoute(['common/upload-thumb', 'event' => 1, 'type' => 'excel', 'dir' => 'import'])?>',
            type: 'post',       //提交的方式
            secureuri: false,   //是否启用安全提交
            fileElementId: 'upload_child_attach',     //file控件ID
            dataType: 'json',  //服务器返回的数据类型
            success: function (data, status) {  //提交成功后自动执行的处理函数
                if (data.error) {
                    alert(data.msg);
                    return false;
                } else {
                    $("#file_path").val(data.fileUrl);
                }
            },
            error: function (data, status, e) {   //提交失败自动执行的处理函数
                alert(e);
            }
        })
    })

    $("#submit_btn").unbind('click').bind('click', function(e){
        var fileUrl = $("#file_path").val();
        if (fileUrl == '') {
            alert('请选择在上传的文件');
            return false;
        }
        $.ajax({
            url: '<?=Yii::$app->urlManager->createUrl(['exhibition/import'])?>',
            data: {
                fileUrl: fileUrl
            },
            type: 'POST',
            dataType: 'JSON',
            success: function (response) {
                if (response.code == 0) {
                    $("#msg_success").html(response.msg);
                    $("#msg_success").removeClass('hidden');
                    $("#file_path").val('');
                }else{
                    if (typeof response.errmsg != 'undefined' && response.errmsg != '') {
                        $("#msg_failure").html(response.errmsg);
                    }else{
                        $("#msg_failure").html(response.msg);
                    }
                    $("#msg_failure").removeClass('hidden');
                    $("#file_path").val('');
                }
            },
            error: function (error) {

            }
        })
    })
})
</script>